package com.taobao.txc.resourcemanager.b;

import com.alibaba.druid.pool.DruidDataSource;
import com.taobao.txc.common.LoggerInit;
import com.taobao.txc.common.LoggerWrap;
import com.taobao.txc.common.TxcContext;
import com.taobao.txc.common.b.c;
import com.taobao.txc.common.j;
import com.taobao.txc.parser.b.a.g;
import com.taobao.txc.parser.struct.TxcField;
import com.taobao.txc.parser.struct.TxcRuntimeContext;
import com.taobao.txc.parser.struct.TxcTable;
import com.taobao.txc.resourcemanager.a.e;
import com.taobao.txc.resourcemanager.k;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import javax.sql.DataSource;

public abstract class f
  implements com.taobao.txc.resourcemanager.b.a.a
{
  private static final LoggerWrap d = LoggerInit.logger;
  protected final Connection a;
  protected final com.taobao.txc.resourcemanager.b.a.b b;
  protected TxcRuntimeContext c = null;
  private final com.taobao.txc.resourcemanager.b e = k.c();
  
  public f(Connection paramConnection, com.taobao.txc.resourcemanager.b.a.b paramb)
  {
    this.a = paramConnection;
    this.b = paramb;
  }
  
  public com.taobao.txc.resourcemanager.b.a.b a()
  {
    return this.b;
  }
  
  public Connection b()
  {
    return this.a;
  }
  
  public TxcRuntimeContext c()
  {
    if (this.c == null)
    {
      this.c = new TxcRuntimeContext();
      this.c.a(TxcContext.getCurrentXid());
    }
    return this.c;
  }
  
  public String a(TxcTable paramTxcTable)
  {
    if (paramTxcTable.b() == 0) {
      return null;
    }
    StringBuilder localStringBuilder = new StringBuilder();
    localStringBuilder.append(paramTxcTable.h());
    localStringBuilder.append(":");
    int i = 0;
    Iterator localIterator = paramTxcTable.d().iterator();
    while (localIterator.hasNext())
    {
      TxcField localTxcField = (TxcField)localIterator.next();
      if (i != 0) {
        localStringBuilder.append(",");
      } else {
        i = 1;
      }
      localStringBuilder.append(localTxcField.c());
    }
    return localStringBuilder.toString();
  }
  
  public void a(com.taobao.txc.resourcemanager.b.a.d paramd, String paramString, g paramg)
  {
    if ((paramString == null) || (paramString.isEmpty())) {
      return;
    }
    String str1 = null;
    try
    {
      str1 = TxcContext.getCurrentXid();
      long l = j.a(str1);
      String str2 = a().b();
      this.e.a(str2, l, paramString);
    }
    catch (Throwable localThrowable)
    {
      a(localThrowable, paramString);
    }
    d.info(String.format("[%d] queryReadLocks ok [%s] [%s] [%s] [%s]", new Object[] { Long.valueOf(TxcContext.d()), paramg.j().d(), paramString, str1, paramd.h() }));
  }
  
  public TxcRuntimeContext a(String paramString)
  {
    e();
    TxcRuntimeContext localTxcRuntimeContext = c();
    try
    {
      if (!localTxcRuntimeContext.h())
      {
        String str1 = a().b();
        long l = this.e.a(str1, paramString, com.taobao.txc.common.a.a);
        localTxcRuntimeContext.d(l);
      }
    }
    catch (Throwable localThrowable)
    {
      String str2 = String.format("registTrxBranch error %s [%s], message:%s", new Object[] { localTxcRuntimeContext.a(), paramString, localThrowable.getMessage() });
      if (paramString == null) {
        d.a("", str2, localThrowable);
      }
      throw new SQLException(str2, localThrowable);
    }
    finally
    {
      d.info(String.format("[%d:%d] regist branch [%s], cost %d ms.", new Object[] { Long.valueOf(TxcContext.d()), Long.valueOf(localTxcRuntimeContext.b()), paramString, Long.valueOf(localTxcRuntimeContext.i()) }));
    }
    return localTxcRuntimeContext;
  }
  
  public TxcRuntimeContext b(String paramString)
  {
    TxcRuntimeContext localTxcRuntimeContext = null;
    try
    {
      localTxcRuntimeContext = a(paramString);
    }
    catch (Throwable localThrowable)
    {
      a(localThrowable, paramString);
    }
    return localTxcRuntimeContext;
  }
  
  public TxcRuntimeContext c(String paramString)
  {
    e locale = new e();
    TxcRuntimeContext localTxcRuntimeContext = null;
    for (;;)
    {
      try
      {
        localTxcRuntimeContext = b(paramString);
      }
      catch (com.taobao.txc.common.b.d locald)
      {
        locale.a(locald);
      }
    }
    return localTxcRuntimeContext;
  }
  
  public void a(boolean paramBoolean)
  {
    e();
    TxcRuntimeContext localTxcRuntimeContext = c();
    try
    {
      String str1 = a().b();
      this.e.a(localTxcRuntimeContext.b(), paramBoolean, str1, null);
    }
    catch (Throwable localThrowable)
    {
      String str2 = String.format("reportBranchStatus error %s:%d", new Object[] { localTxcRuntimeContext.a(), Long.valueOf(localTxcRuntimeContext.b()) });
      d.a("", str2, localThrowable);
      throw new SQLException(str2, localThrowable);
    }
    finally
    {
      d.info(String.format("[%d:%d] report %b, cost %d ms.", new Object[] { Long.valueOf(TxcContext.d()), Long.valueOf(localTxcRuntimeContext.b()), Boolean.valueOf(paramBoolean), Long.valueOf(localTxcRuntimeContext.i()) }));
    }
  }
  
  private void e()
  {
    if (!TxcContext.inTxcTransaction()) {
      throw new SQLException("should 'set txc env' first.");
    }
  }
  
  private void a(Throwable paramThrowable, String paramString)
  {
    String str = TxcContext.getCurrentXid();
    Throwable localThrowable = paramThrowable.getCause();
    if (localThrowable == null) {
      localThrowable = paramThrowable;
    }
    if ((localThrowable != null) && ((localThrowable instanceof c)))
    {
      c localc = (c)localThrowable;
      if (localc.getMessage().contains("query lock"))
      {
        d.info(String.format("[%d] Lock confict [%s] [%s]", new Object[] { Long.valueOf(TxcContext.d()), paramString, str }));
        throw new com.taobao.txc.common.b.d(paramThrowable);
      }
      if (localc.getMessage().contains("get local lock"))
      {
        d.info(String.format("[%d] Lock confict [%s] [%s]", new Object[] { Long.valueOf(TxcContext.d()), paramString, str }));
        throw new com.taobao.txc.common.b.d(paramThrowable);
      }
      if (localc.getMessage().contains("cluster operation fail"))
      {
        d.info(String.format("[%d] Lock confict cluster [%s] [%s]", new Object[] { Long.valueOf(TxcContext.d()), paramString, str }));
        throw new com.taobao.txc.common.b.d(paramThrowable);
      }
    }
    d.info("error message:", new Object[] { localThrowable.getMessage() });
    if ((paramThrowable instanceof SQLException)) {
      throw ((SQLException)paramThrowable);
    }
    throw new SQLException(paramThrowable);
  }
  
  public d d()
  {
    try
    {
      DataSource localDataSource = this.b.a();
      if ((localDataSource instanceof DruidDataSource))
      {
        String str1 = ((DruidDataSource)localDataSource).getDbType();
        if ("mysql".equals(str1)) {
          return d.b;
        }
        if ("oracle".equals(str1)) {
          return d.a;
        }
        if ("petadata".equalsIgnoreCase(str1)) {
          return d.f;
        }
        if ("oceanbase".equals(str1)) {
          return d.d;
        }
        if ("h2".equals(str1)) {
          return d.e;
        }
        if ("postgresql".equals(str1)) {
          return d.c;
        }
        String str2 = b().getMetaData().getDatabaseProductName();
        if ("PostgreSQL".equalsIgnoreCase(str2)) {
          return d.c;
        }
      }
    }
    catch (Throwable localThrowable)
    {
      d.error("getDsType error", localThrowable.getMessage());
    }
    return d.b;
  }
}


/* Location:           C:\Users\ibm\Downloads\txc-yun-sample (1)\txc-yun-sample\lib\txc-client-2.0.72.jar
 * Qualified Name:     com.taobao.txc.resourcemanager.b.f
 * JD-Core Version:    0.7.0.1
 */